package com.ctshk.sapp.bus.controller;

import java.util.List;

import javax.validation.Valid;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.bus.dto.BusProductDTO;
import com.ctshk.rpc.bus.dto.BusProductPageDTO;
import com.ctshk.rpc.bus.dto.BusProductReservationAgentDTO;
import com.ctshk.rpc.bus.dto.BusSeatDTO;
import com.ctshk.rpc.bus.dto.RecommendedBusDTO;
import com.ctshk.rpc.bus.dto.ReservationBusPageDTO;
import com.ctshk.rpc.bus.req.BatchBusProductInfoReq;
import com.ctshk.rpc.bus.req.BusProductAddApprovalReq;
import com.ctshk.rpc.bus.req.BusProductListForIotcQueryReq;
import com.ctshk.rpc.bus.req.BusProductQueryReq;
import com.ctshk.rpc.bus.req.BusProductReq;
import com.ctshk.rpc.bus.req.BusProductReservationAgentQueryReq;
import com.ctshk.rpc.bus.req.BusProductReservationSettingsReq;
import com.ctshk.rpc.bus.req.BusProductSetReq;
import com.ctshk.rpc.bus.req.ReservationBusDetailReq;
import com.ctshk.rpc.bus.req.ReservationBusPageReq;
import com.ctshk.rpc.bus.service.IBusProductAppService;
import com.ctshk.rpc.bus.service.IBusProductService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 巴士产品表 前端控制器
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-02-08
 */
@Api(value="资源中心-巴士产品接口", tags="资源中心-巴士产品接口")
@RestController
@RequestMapping("/bus-product")
public class BusProductController extends BaseController {

    @DubboReference
    private IBusProductService busProductService;
    
    @DubboReference
    IBusProductAppService busProductAppService;

    @ApiOperation(value = "巴士票管理列表查询", notes = "巴士票管理列表查询")
    @PostMapping(value = "/query-list")
    public Result<PageResponse<BusProductPageDTO>> queryList(@RequestBody BusProductQueryReq req) {
        return busProductService.queryList(req, getCurrentUser());
    }

    @ApiOperation(value="新增巴士产品", notes="新增巴士产品")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.ADD, operTitle = "新增巴士产品", operDesc = "新增巴士产品")
    @PostMapping("/save")
    public Result saveBusProduct(@RequestBody @Valid BusProductReq busProductReq) {
        TokenUser user = getCurrentUser(getRequest());
        return busProductService.saveBusProduct(busProductReq,user);
    }

    @ApiOperation(value="新增巴士产品预订信息", notes="新增巴士产品预订信息")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.ADD, operTitle = "新增巴士产品预订信息", operDesc = "新增巴士产品预订信息")
    @PostMapping("/saveReservation")
    public Result saveReservation(@RequestBody @Valid BusProductReservationSettingsReq reservationSettingsReq) {
        TokenUser user = getCurrentUser(getRequest());
        return busProductService.saveReservation(reservationSettingsReq,user);
    }

    @ApiOperation(value="修改产品预订信息", notes="修改产品预订信息")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "修改产品预订信息", operDesc = "修改产品预订信息")
    @PostMapping("/updateReservation")
    public Result updateReservation(@RequestBody @Valid BusProductReservationSettingsReq reservationSettingsReq) {
        TokenUser user = getCurrentUser(getRequest());
        return busProductService.updateReservation(reservationSettingsReq,user);
    }


    @ApiOperation(value = "新增-编辑提交审批", notes = "新增-编辑提交审批")
    @GetMapping(value = "/update-approval-status/{id}")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "巴士产品提交审批", operDesc = "巴士产品提交审批")
    public Result submit(@PathVariable("id") Long id) {
        TokenUser user = getCurrentUser(getRequest());
        return busProductService.submit(id,user);
    }


    @ApiOperation(value="修改审核通过巴士产品", notes="修改审核通过巴士产品")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "修改审核通过巴士产品", operDesc = "修改审核通过巴士产品")
    @PostMapping("/approved/update")
    public Result updateApprovedBusProduct(@RequestBody @Valid BusProductReq busProductReq) {
        TokenUser user = getCurrentUser(getRequest());
        return busProductService.updateApprovedBusProduct(busProductReq,user);
    }

    @ApiOperation(value="修改审核不通过巴士产品", notes="修改审核不通过巴士产品")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "修改审核不通过巴士产品", operDesc = "修改审核不通过巴士产品")
    @PostMapping("/unapproved/update")
    public Result updateUnapprovedBusProduct(@RequestBody @Valid BusProductReq busProductReq) {
        TokenUser user = getCurrentUser(getRequest());
        return busProductService.updateUnapprovedBusProduct(busProductReq,user);
    }



    @ApiOperation(value = "编辑时查询巴士产品详情", notes = "编辑时查询巴士产品详情")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "编辑时查询巴士路线详情", operDesc = "编辑时查询巴士路线详情")
    @GetMapping(value = "/detail")
    public Result<BusProductDTO> queryDetail(@RequestParam(value = "id", required = true) Long id) {
        return Result.success(busProductService.queryDetail(id));
    }


    @ApiOperation(value = "查询巴士线路座位信息", notes = "查询巴士线路座位信息")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "查询座位", operDesc = "查询座位")
    @GetMapping(value = "/querySeats")
    public Result<BusSeatDTO> querySeats(@RequestParam(value = "lineId", required = true) Long lineId) {
        return Result.success(busProductService.querySeats(lineId));
    }

    @ApiOperation(value = "API设置", notes = "API设置")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "API设置", operDesc = "API设置")
    @PostMapping(value = "/setApi")
    public Result setApi(@RequestBody @Valid BusProductSetReq busProductSetReq) {
        TokenUser user = getCurrentUser(getRequest());
        return busProductService.setApi(busProductSetReq,user);

    }

    @ApiOperation(value = "有效无效设置", notes = "有效无效设置")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "有效无效设置", operDesc = "有效无效设置")
    @GetMapping("/setValid/{id}")
    public Result setValid(@PathVariable("id") Long id) {
        TokenUser user = getCurrentUser(getRequest());
        return busProductService.setValid(id,user);

    }

    @ApiOperation(value = "批量设置", notes = "批量设置")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "有效无效设置", operDesc = "有效无效设置")
    @PostMapping(value = "/batchSetting")
    public Result batchSetting(@RequestBody @Valid BatchBusProductInfoReq batchBusProductInfoReq) {
        TokenUser user = getCurrentUser(getRequest());
        return busProductService.batchSetting(batchBusProductInfoReq,user);

    }


    @ApiOperation(value = "推荐设置", notes = "推荐设置")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "推荐设置", operDesc = "推荐设置")
    @GetMapping(value = "/setRecommended/{id}")
    public Result setRecommended(@PathVariable("id") Long id) {
        TokenUser user = getCurrentUser(getRequest());
        return busProductService.setRecommended(id,user);
    }



    @ApiOperation(value = "查询巴士票推荐列表", notes = "查询巴士票推荐列表")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "查询巴士票推荐列表", operDesc = "查询巴士票推荐列表")
    @PostMapping(value = "/recommendedBus/list")
    public Result<List<RecommendedBusDTO>> queryRecommendedBusTicket() {
        Long userId = getCurrentUser(getRequest()).getId();
        List<RecommendedBusDTO> reservationBusDTOList = busProductService.queryRecommendedBusTicket(userId);
        return Result.success(reservationBusDTOList);
    }


    @ApiOperation(value = "巴士票预订查询列表", notes = "巴士票预订查询列表")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "巴士票预订查询列表", operDesc = "巴士票预订查询列表")
    @PostMapping(value = "/reservationBus/list")
    public Result<PageResponse<ReservationBusPageDTO>> queryReservationBusList(@RequestBody @Valid ReservationBusPageReq req) {
        Long userId = getCurrentUser(getRequest()).getId();
        return Result.success(busProductService.queryReservationBusList(req,userId));
    }
    
    @ApiOperation(value = "巴士票预订详情", notes = "巴士票预订详情")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "巴士票预订详情", operDesc = "巴士票预订详情")
    @PostMapping(value = "/reservationBus/detail")
    public Result<ReservationBusPageDTO> queryReservationBusDetail(@RequestBody @Valid ReservationBusDetailReq req) {
        return busProductService.queryReservationBusDetail(req, getCurrentUser());
    }


    @ApiOperation(value = "中港通API产品导入", notes = "中港通API产品导入")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "中港通API产品导入", operDesc = "中港通API产品导入")
    @PostMapping(value = "/importHktApi")
    public Result importZgtApi() throws Exception {
        busProductService.importHktBusProduct();
        return Result.success();
    }


    @ApiOperation(value = "中汽API产品导入", notes = "中汽API产品导入")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "中汽API产品导入", operDesc = "中汽API产品导入")
    @PostMapping(value = "/importRglApi")
    public Result importRglApi() throws Exception {
        busProductService.importRglBusProduct();
        return Result.success();
    }
    
    @ApiOperation("巴士新增产品审批")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.OTHER, operTitle = "巴士新增产品审批", operDesc = "巴士新增产品审批")
    @PostMapping(value = "/approval")
    public Result<Void> approval(@RequestBody @Valid BusProductAddApprovalReq req) {
		return busProductService.approval(req, getCurrentUser());
	}
    
    @ApiOperation("运营中心查询巴士产品列表")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "运营中心查询巴士产品列表", operDesc = "运营中心查询巴士产品列表")
    @PostMapping("queryProductListForIotc")
    public Result<PageResponse<BusProductDTO>> queryProductListForIotc(@RequestBody BusProductListForIotcQueryReq req) {
		return busProductService.queryProductListForIotc(req);
	}
    
    @ApiOperation("查询预订用的代理商信息列表")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.QUERY, operTitle = "查询预订用的代理商信息列表", operDesc = "查询预订用的代理商信息列表")
    @PostMapping("queryReservationAgentList")
    public Result<List<BusProductReservationAgentDTO>> queryReservationAgentList(@RequestBody @Valid BusProductReservationAgentQueryReq req) {
		return busProductService.queryReservationAgentList(req, getCurrentUser());
	}
    
    @ApiOperation("（内部用）同步站点主数据")
    @PostMapping("synStopsData")
    public Result<Void> synStopsData() {
    	return busProductAppService.synStopsData();
    }
}
